package com.yc.book.biz;


import com.yc.book.dao.BookDao;
import com.yc.book.util.Utils;
import com.yc.common.biz.BizException;
import com.yc.common.util.DBHelper;

public class BookBiz {
    private BookDao dao = new BookDao();

    public void save(String id, String bookname, String bookpress, String pressdate, String bookauthor, String bookcount, String bookimage) throws BizException {
        Utils.checkEmpty(bookname, "书名不能为空!");
        Utils.checkEmpty(bookpress, "出版社不能为空!");
        Utils.checkEmpty(bookauthor, "作者不能为空!");

        Utils.check(bookauthor.length() < 2, "作者名字不能少于2个字符!");
        Utils.check(pressdate.matches("\\d{4}-\\d{1,2}-\\d{1,2}") == false, "出版时间必须是'yyyy-MM-dd'!");

        Utils.check(dao.countByName(bookname, id) > 0, "该书已经录入了系统");


        if (id == null) {
            dao.insert(bookname, bookpress, pressdate, bookauthor, bookcount, bookimage);
        } else {
            dao.update(id, bookname, bookpress, pressdate, bookauthor, bookcount, bookimage);
        }
    }

    public long countByName(String bookname) {
        return DBHelper.count("select * from books where bookname = ? ");
    }
}
